<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 一帆风顺
 */
class YifanController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.ordNo") ? I("request.ordNo") : I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        // $isMobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            case 1:
            case 2:
                $pay_bankcode = '930';
                break;

            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $native = array(
            "pay_memberid" => $this->config['mch_id'],
            "pay_orderid" => $return['orderid'],
            "pay_amount" => number_format($return['amount'],2,'.',''),
            "pay_applydate" =>  date("Y-m-d H:i:s"),
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $return['notifyurl'],
            "pay_callbackurl" => $return['callbackurl'],
        );
        ksort($native);

        $md5str = httpstr($native). "&key=" . $this->config['signkey'];
        $sign = strtoupper(md5($md5str));

        $native["pay_md5sign"] = $sign;
        $native['pay_attach'] = "1234|456";
        $native['pay_productname'] ='团购商品';


        //var_dump('<pre/>',$native,'返回数据：',$this->config['gateway']);exit();

        po($this->config['gateway'], $native);
    }



    //通知
    public function notifyurl(){
        //$this->debug_callback();

        //IP白名单
        if(!$this->checkip(getIP(),['139.180.202.94'])){
           // exit("wrong");
        }

        $p = I('post.');


        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['orderid'];

        //获取订单
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据

        if($p['returncode'] === '00'){
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

                "pay_orderid_rd"=>htmlspecialchars($p["transaction_id"]),
                "pay_true"=>$p['amount'],
            ));
        }else{
            M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

                "pay_orderid_rd"=>htmlspecialchars($p["transaction_id"]),
            ));
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];

        $returnArray = array( // 返回字段
            "memberid" => $p["memberid"], // 商户ID
            "orderid" =>  $p["orderid"], // 订单号
            "amount" =>  $p["amount"], // 交易金额
            "datetime" =>  $p["datetime"], // 交易时间
            "transaction_id" =>  $p["transaction_id"], // 支付流水号
            "returncode" => $p["returncode"],
        );

        ksort($returnArray);
        reset($returnArray);

        $md5str = httpstr($returnArray). "&key=" . $this->config['signkey'];
        $sign2 = strtoupper(md5($md5str));


        if($sign !== $sign2){
            exit("sign_wrong");
        }

        if($p['returncode'] !== '00'){
            exit("pay_fail");
        }


        $this->async($find['pay_orderid'],"OK");
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}
